//
//  YLLPhotoView.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/8/3.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// 配图视图距离 view的间距
private let YLLPhotoViewMargin: CGFloat = 10
// 配图里的cell/item他的间距
private let YLLPhotoItemMargin: CGFloat = 10
// 配图里的Cell/Item的宽和高
private let YLLPhotoItemWH: CGFloat = (UIScreen.mainScreen().bounds.width - 2 * YLLPhotoViewMargin - 2 * YLLPhotoItemMargin) / 3

private let YLLPhotoViewReuseIdentifier = "YLLPhotoViewReuseIdentifier"


class YLLPhotoView: UICollectionView {

    var pic_urls:[YLLPhotoModel]?{
        
        didSet{
            
            let size = calcViewSize()
            
            self.snp_updateConstraints { (make) in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            reloadData()
        }
        
    }
 
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func calcViewSize() -> CGSize{
        
        let count = pic_urls?.count
        
        
        
        if count == 1 {
            
            // 这个属于一个特殊情况,我们可以先给他一个随便的值
            return CGSizeMake(YLLPhotoItemWH, YLLPhotoItemWH)
        }
        
        if count == 4 {
            
            let wh = YLLPhotoItemWH * 2 + YLLPhotoItemMargin
            
            return CGSizeMake(wh, wh)
        }
        
        // 2 3
        if count == 2 || count == 3 {
            
            
            return CGSizeMake(UIScreen.mainScreen().bounds.width - 2 * YLLPhotoViewMargin, YLLPhotoItemWH)
        }
        // 5 6
        if count == 5 || count == 6 {
            
            return CGSizeMake(UIScreen.mainScreen().bounds.width - 2 * YLLPhotoViewMargin, (YLLPhotoItemWH + YLLPhotoItemMargin ) * 2 )
            
        }
        //7 8 9
        
        if count == 7 || count == 8 || count == 9 {
            return CGSizeMake(UIScreen.mainScreen().bounds.width - 2 * YLLPhotoViewMargin, (YLLPhotoItemWH + YLLPhotoItemMargin ) * 3 )
            
        }
        
        return CGSizeZero
        //        return CGSizeMake(200, 200)
    }

    
    private func setupUI(){
        
        dataSource = self
        
        self.registerClass(YLLPhotoViewCell.self, forCellWithReuseIdentifier: YLLPhotoViewReuseIdentifier)
        
        flowLayout.itemSize = CGSizeMake(YLLPhotoItemWH, YLLPhotoItemWH)
        flowLayout.minimumLineSpacing = YLLPhotoItemMargin
        flowLayout.minimumInteritemSpacing = YLLPhotoItemMargin
        
        
    }
    
}

extension YLLPhotoView : UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YLLPhotoViewReuseIdentifier, forIndexPath: indexPath) as! YLLPhotoViewCell
        
        cell.model = pic_urls![indexPath.item]
        
        return cell
        
    }
    
    
}


class YLLPhotoViewCell: UICollectionViewCell {
    
    var model: YLLPhotoModel?{
        
        didSet{
            iconImageView.sd_setImageWithURL(NSURL(string: model?.thumbnail_pic ?? ""))
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        contentView.addSubview(iconImageView)
        
        iconImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        
    }
    
    private lazy var iconImageView:UIImageView = {
        
        let img = UIImageView()
        
        return img
        
        
    }()
    
    
}